package cn.bhygzs.mqtt_yw_aidl__demo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.telephony.PhoneNumberUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.UnsupportedEncodingException;

import bhygzs.cn.mqtt_client.Imqttbgret;
import bhygzs.cn.mqtt_client.Imqttbgset;

/****
 *  本程序为你演示了MQTT_yw的aidl简单调用,你可以构建更丰富的界面
 *  在使用aid时，请务必先安装MQTT_yw的APP,相关资料：https://www.bhygzs.cn/page/11.html
 *  由于手机厂商限制，部分手机，使用aidl时，需要先启动MQTT_yw，后台运行即可。
 *
 *  随手写的demo，比较随意，实际开发请注意调用生命周期和容错处理。
 */

public class MainActivity extends AppCompatActivity {

    private Imqttbgset imqttbgset;
    private static final String TAG = "MainActivity";
    private Button bt_jstopic, bt_fstopic;
    private EditText et_jstopic, et_fstopic, et_fstopic_mage;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        onBind();

        /**
         * 设置接收数据的主题，注意判空，空数据就挂了
         */
        bt_jstopic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String topic = et_jstopic.getText().toString().trim();
                if (topic != null && imqttbgset != null) {
                    try {
                        imqttbgset.set_retopic(topic);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        /**
         *指定主题发送信息，注意判空，空数据就挂了
         */
        bt_fstopic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String topic = et_fstopic.getText().toString().trim();
                String mage = et_fstopic_mage.getText().toString().trim();
                if (topic != null && mage != null && imqttbgset != null) {
                    try {
                        imqttbgset.fs_toserver(topic, mage);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
        });


    }

    /***
     * 控件初始化，如果你不是独立开发，id请不要这样命名，哈哈
     */
    private void initView() {
        bt_jstopic = findViewById(R.id.bt1);
        bt_fstopic = findViewById(R.id.bt2);
        et_jstopic = findViewById(R.id.et1);
        et_fstopic = findViewById(R.id.et2);
        et_fstopic_mage = findViewById(R.id.et3);
        textView = findViewById(R.id.tv);
    }

    /***
     * 绑定MQTT_yw的Aidl服务*核心部分
     */
    private void onBind() {
        Intent intent = new Intent();
        intent.setAction("bhygzs.cn.mqtt_client.AIDL");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setPackage("bhygzs.cn.mqtt_client");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        bindService(intent, new AidlConnection(), BIND_AUTO_CREATE);
    }

    /**
     * 接收回调
     * retServermsg--》返回状态信息
     * retTopicmsg---》返回订阅主题的信息
     */
    private class callback extends Imqttbgret.Stub {
        @Override
        public void retServermsg(String ss) throws RemoteException {
            Log.d(TAG, "retServermsg: " + ss);
        }

        @Override
        public void retTopicmsg(final String ss) throws RemoteException {
            Log.d(TAG, "retTopicmsg: " + ss);
            MainActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    textView.append(ss+"\n");
                }
            });
        }
    }


    private class AidlConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            imqttbgset = Imqttbgset.Stub.asInterface(service);
            try {
                /**
                 * 连接mqtt服务器
                 * url为mqtt服务器地址，记得加tcp://和端口号
                 */
                imqttbgset.connect("tcp://192.168.3.102:1883", "bhygzsclinet2", new callback());
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

}
